package com.sinux.sshUser.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinux.sshUser.dao.DepartmentDaoI;
import com.sinux.sshUser.dao.UserDaoI;
import com.sinux.sshUser.entity.Department;
import com.sinux.sshUser.entity.Role;
import com.sinux.sshUser.entity.User;
import com.sinux.sshUser.service.DepartmentServiceI;

/**
 * @author WangGan
 * @version 2017年5月25日 下午4:47:17 
 * 
 */
@Service
public class DepartmentServiceImpl implements DepartmentServiceI {
	
	@Autowired
	private DepartmentDaoI departDao;
	@Autowired
	private UserDaoI userDao;
	
	
	@Override
	public Department findDepart(String deid) {
		String hql="from Department t where t.id=:deid";
		Map<String, Object> params=new HashMap<>();
		params.put("deid", deid);
		Department depart =departDao.get(hql, params);
		return depart;
	}
	
	@Override
	public List<Department> findDeparts(String depid) {
		String hql="from Department t where t.depid=:depid";
		Map<String, Object> params=new HashMap<>();
		params.put("depid", depid);
		List<Department> departs =departDao.find(hql, params);
		return departs;
	}
	@Override
	public List<Department> findAllDepartList() {
		List<Department> departs= departDao.find("from Department t");
		return departs;
	}
	@Override
	public List<Department> findDepartList(String value) {
		String hql="from Department u where (u.deid like '%"+value+"%' or u.dename like '%"+value+"%' or u.detype like '%"+value+"%')";
		List<Department> departs= departDao.find(hql);
		return departs;
	}
	@Override
	public List<Department> findDepartList(String value,int page, int rows) {
		String hql="from Department u where (u.deid like '%"+value+"%' or u.dename like '%"+value+"%' or u.detype like '%"+value+"%')";
		List<Department> departs= departDao.find(hql,page,rows);
		return departs;
	}
	
	@Override
	public List<Department> findDepartPageing(int page, int rows){
		String hql="from Department u ";
		List<Department> depart=departDao.find(hql, page, rows);
		return depart;
	}
	@Override
	public void addDepartment(Department depart) {
		departDao.save(depart);	
	}
	
	@Override 
	public List<User> findDepartUsers(String deid, int page, int rows) {//page:第几页    rows:每页多少条
	    String hql="SELECT a FROM User a LEFT JOIN a.departs b WHERE b.id = "+deid+" order by a.uid ASC";
		List<User> users=userDao.find(hql, page, rows);
		return users;
	}

	@Override
	public boolean removeDepartUsers(String[] uids, String deid) {
		String hql="from Department d where d.id="+deid;
		Department depart=departDao.get(hql);//获取部门
		Set<User> users=depart.getUsers();//获取部门的用户
		List<User> list=new ArrayList<>();
		for(int i=0;i<uids.length;i++){
			//2.根据deid获取部门,
			User user=userDao.get("from User where uid="+uids[i]);//获取指定用户
			list.add(user);		
		}
		//3.移除用户
		boolean isTag=users.removeAll(list);//移除指定用户
		return isTag;
		
	}
	
	
	
	
}
